package com.gopay.bis.manager.bank.bcdl;
import java.util.Date;
import java.util.Map;
import javax.annotation.Resource;
import com.gopay.common.domain.bank.ccb.bcdl.querydcc.BcdlDccMessage;
import com.gopay.common.domain.bank.ccb.bcdl.querydcc.req.TransReqQuerydcc6WY101;
import com.gopay.common.domain.bank.ccb.bcdl.querydcc.res.TransResQueryBody6WY101;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BcdlMessage;
import com.gopay.common.domain.bank.ccb.bcdl.BaseReqBody;
import com.gopay.common.domain.bank.ccb.bcdl.Trans;
import com.gopay.common.domain.bank.ccb.bcdl.corp.req.TransReqCorp;
import com.gopay.common.domain.bank.ccb.bcdl.corp.req.TransReqCorpBody;
import com.gopay.common.domain.bank.ccb.bcdl.corp.res.TransResCorpBody;
import com.gopay.common.domain.bank.ccb.bcdl.person.req.TransReqPerson;
import com.gopay.common.domain.bank.ccb.bcdl.person.req.TransReqPersonBody;
import com.gopay.common.domain.bank.ccb.bcdl.person.res.TransResPersonBody;
import com.gopay.common.domain.bank.ccb.bcdl.query.req.TransReqQuery;
import com.gopay.common.domain.bank.ccb.bcdl.query.res.TransResQueryBody;
import com.gopay.common.domain.bank.ccb.bcdl.querybalance.req.TransReqQueryBalance;
import com.gopay.common.domain.bank.ccb.bcdl.querybalance.res.TransResQueryBalanceBody;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.bank.businesses.BcdlService;

/**
 * @author lianpeng
 *	建行银企直连拼装对象
 */
public class CcbBcdlManager {
	@Resource(name = "ccbBcdlService")
	private BcdlService ccbBcdlService;


	@Resource(name = "ccbConfig")
	private Map<String, String> ccbConfig;

	public TransResCorpBody payCorp(BcdlMessage message) throws GopayException{
		BaseReqBody req = convertCorp(message);
		BankLogUtils.printBcdlEftManagerSendLog(req);
		TransResCorpBody result = (TransResCorpBody) ccbBcdlService.eft(req);
		BankLogUtils.printBcdlEftManagerReturnLog(result);
		return result;
	}

	public TransResPersonBody payPerson(BcdlMessage message) throws GopayException{
		BaseReqBody req = convertPerson(message);
		BankLogUtils.printBcdlEftManagerSendLog(req);
		TransResPersonBody result = (TransResPersonBody) ccbBcdlService.eft(req);
		BankLogUtils.printBcdlEftManagerReturnLog(result);
		return result;
	}

	public TransResQueryBody payQuery(BcdlMessage message) throws GopayException{
		BaseReqBody req = convertQuery(message);
		BankLogUtils.printBcdlEftManagerSendLog(req);
		TransResQueryBody result = (TransResQueryBody) ccbBcdlService.query(req);
		BankLogUtils.printBcdlEftManagerReturnLog(result);
		return result;
	}

	public TransResQueryBalanceBody queryBal(BcdlMessage message) throws GopayException{
		BaseReqBody req = convertQueryBal(message);
		BankLogUtils.printBcdlEftManagerSendLog(req);
		TransResQueryBalanceBody result = (TransResQueryBalanceBody) ccbBcdlService.queryBalance(req);
		BankLogUtils.printBcdlEftManagerReturnLog(result);
		result.setPayAccNum(ccbConfig.get("CCB.BCDL.PAY.ACCOUNTNO"));
		return result;
	}

	private BaseReqBody convertQueryBal(BcdlMessage message) {
		// TODO Auto-generated method stub
		BaseReqBody body = new BaseReqBody();
		buildReq(body, new TransReqQueryBalance(ccbConfig.get("CCB.BCDL.PAY.ACCOUNTNO")), message, null);
		return body;
	}

	private BaseReqBody convertQuery(BcdlMessage message) {
		// TODO Auto-generated method stub
		BaseReqBody body = new BaseReqBody();
		buildReq(body, new TransReqQuery(message.getOrderId()), message, null);
		return body;
	}

	private BaseReqBody convertPerson(BcdlMessage message){
		TransReqPerson trans = new TransReqPerson();
		trans.setAccNo1(ccbConfig.get("CCB.BCDL.PAY.ACCOUNTNO"));
		trans.setAccNo2(message.getRecvBankAcctNum());
		trans.setAmount(message.getAmount().toString());
		trans.setBillCode(ccbConfig.get("CCB.BCDL.BILL.CODE"));
		trans.setOtherName(message.getRecvBankAcctName());
		trans.setFlowFlag("0");
		trans.setUbankNo("");
		trans.setUseofCode(ccbConfig.get("CCB.BCDL.USEOF.CODE"));
		trans.setRem1(message.getOrderId());
		trans.setRem2(message.getOrderId());
		TransReqPersonBody body = new TransReqPersonBody();
		buildReq(body,trans,message,message.getOrderId());
		body.setDcblType(BankConstants.DCBL_PERSON);
		return body;
	}

	private BaseReqBody convertCorp(BcdlMessage message) {
		// TODO Auto-generated method stub
		TransReqCorp trans = new TransReqCorp();
		trans.setPayAccNo(ccbConfig.get("CCB.BCDL.PAY.ACCOUNTNO"));
		trans.setRecvAccNo(message.getRecvBankAcctNum());
		trans.setRecvAccName(message.getRecvBankAcctName());
		trans.setChkRecvName("1");//行内转账收款账户户名校验：1校验 0不校验
		trans.setRecvOpenAccDept(message.getPbocBankName());
		trans.setAmount(message.getAmount().toString());
		trans.setCurType("01");//01人民币
		trans.setCstPayNo(message.getOrderId());
		trans.setUseof(message.getOrderId());
		trans.setRem1(message.getOrderId());
		trans.setRem2(message.getOrderId());
		TransReqCorpBody body = new TransReqCorpBody();
		buildReq(body,trans,message,message.getOrderId());
		body.setDcblType(BankConstants.DCBL_CORP);
		//body.setSignInfo("签名信息");
		//body.setSigncert("签名CA信息");
		return body;
	}

	private void buildReq(BaseReqBody body,Trans trans, BcdlMessage message, String requestSn) {
		// TODO Auto-generated method stub
		BeanUtils.copyProperties(message, body);
		if(StringUtils.isNotBlank(requestSn)){
			body.setRequestSn(requestSn);
		}else{
			body.setRequestSn(DateUtils.format(new Date(), "yyMMddHHmmss"));
		}
		body.setCustId(ccbConfig.get("CCB.BCDL.CUSTID"));
		body.setUserId(ccbConfig.get("CCB.BCDL.CUSOPR"));
		body.setPassword(ccbConfig.get("CCB.BCDL.OPRPWD"));
		body.setLanguage("CN");
		body.setTrans(trans);
	}



	/**
	 * （6WY101）一点接入活期账户明细查询
	 * ssj
	 * @param message
	 * @return
	 *
	 * @throws GopayException
	 */
	public TransResQueryBody6WY101 queryAcctDetail(BcdlMessage message) throws GopayException{

		BcdlDccMessage dccMsg=(BcdlDccMessage)message;
		BaseReqBody body = convertQuer6WY101(message,dccMsg);
		TransResQueryBody6WY101 result = (TransResQueryBody6WY101) ccbBcdlService.queryAcctDetail(body);
		return result;
	}
	 /**
	 * （6WY101）一点接入活期账户明细查询 转换
	 * ssj
	 * @param message
	 * @return
	 * @throws GopayException
	 */
	private BaseReqBody convertQuer6WY101(BcdlMessage message, BcdlDccMessage dccMessage) {
		BaseReqBody body = new BaseReqBody();
		TransReqQuerydcc6WY101 res=new TransReqQuerydcc6WY101();
		res.setAccno1(ccbConfig.get("CCB.BCDL.PAY.ACCOUNTNO"));
		//开始时间
		res.setStartdate(dccMessage.getStartdate());
		//结束时间
		res.setEnddate(dccMessage.getEnddate());
		// 交易方向
		res.setBargain_flag("");
		//对方账户
		res.setCheck_acc_no("");
		// 对方账户名称
		res.setCheck_acc_name("");
		//备注
		res.setRemark("");
		//最小金额
		res.setLow_amt("");
        //最大金额
		res.setHigh_amt("");
		//起始页
		res.setPage("1");
		//定位串
		res.setPoststr("");
		//每页记录数
		res.setTotal_record("200");
		//起始明细号
		res.setDet_no("");
		res.setSender(dccMessage.getSender());
		res.setUsername(dccMessage.getUsername());
		res.setPassword(dccMessage.getPassword());
		res.setHost(dccMessage.getHost());
		res.setReceiver(dccMessage.getReceiver());
		res.setCc(dccMessage.getCc());
		res.setDateType(dccMessage.getDateType());
		res.setQuerySize(dccMessage.getQuerySize());

		buildReq(body, res , message, null);
		return body;
	}



}
